Reverse Linked List II

Medium

Question

Given the head of a linked list and two integers left and right, reverse the nodes of the list from index left (inclusive) to index right (exclusive), and return the head of the list.

Note: The head of the linked list is at index 0.

Reverse Linked List II Example 1

Input: head = [4 -> 7 -> 6 -> 5 -> 1], left = 1, right = 4

Reverse Linked List II Example 1

Output: [4 -> 5 -> 6 -> 7 -> 1]

Reverse Linked List II Example 2

Input: head = [0 -> 1 -> 2 -> 3], left = 2, right = 3

Reverse Linked List II Example 2

Output: [0 -> 1 -> 2 -> 3]

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

What is the correct output if the following is our input? head = [0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6], left = 3, right = 6
[0 -> 1 -> 2 -> 5 -> 4 -> 3 -> 6]
[0 -> 1 -> 2 -> 3 -> 5 -> 4 -> 6]
[0 -> 1 -> 5 -> 4 -> 3 -> 2 -> 6]
[0 -> 1 -> 2 -> 6 -> 5 -> 4 -> 3]

Login or signup to save your code.

Notes